import {
    createI18n
} from "vue-i18n"

import cn from './cn'
import en from './en'

const messages = {
    cn,
    en
}
const i18n = new createI18n({
    locale: getLocaleLang(),
    messages: messages
})
export const t = i18n.global.t

export default {
    install(vue) {
        vue.use(i18n)
    }
}

export function changeLanguage(lang) {
    if (messages[lang]) {
        i18n.global.locale = lang
        window.localStorage.setItem("app-language", lang)
        return true
    } else {
        return false
    }
}

function getLocaleLang() {
    return window.localStorage.getItem("app-language") || 'cn'
}
